#include "network_service.h"
#include "connection.h"
#include "listener.h"
#include "service.h"
#include "database_service.h"
#include "jservice_runner.h"
#include "db_proto.h"
#include "jmsg.h"
#include "GameUtil.h"
struct NetworkServerSendCmd {
	int connId;
	void* data;
	int len;
};

NetworkService::~NetworkService(void)
{
}

void NetworkService::onAccept( Connection* connection )
{
	printf("accept a conn\n");
	connection->setHandler(this);
	m_mapConnectin[connection->getId()] = connection;
}

void NetworkService::onConnected( Connection* conn )
{

}

void NetworkService::onClose( Connection* conn )
{
 
}

void NetworkService::onData( Connection* conn )
{
	int lenInput = conn->getBufferLen();
	if(lenInput < 4) {
		return;
	}

	unsigned char* lenBuf = (unsigned char*)conn->getBuffer(4);
	int lenData = getIntFromBuffer(lenBuf, 4);
	
	if(lenInput < lenData + 4) {
		return;
	}

	char* bufData = (char*)conn->getBuffer(lenData + 4) + 4;
	this->processClientData(bufData, lenData, conn);
	conn->removeBuffer(lenData + 4);
}

NetworkService::NetworkService( void )
{
	m_proto = db_protoCreateProto();
	m_service = new Service;
	m_listener = new Listener("0.0.0.0", 8129, m_service, this);
	m_listener->startListen();
	m_service->setHandler(this);
	m_service->start();

	m_dbService = new DatabaseService;
	m_runner = new JServiceRunner;
	m_runner->StartService2(m_dbService, this);
	m_runner->run(1);

}

void NetworkService::processClientData(void* data, int len, Connection*  conn)
{
	bool isSuccess = true;
	JMsgReader reader((unsigned char*)data, len);
	int msgId = reader.peekMessageTypeId(isSuccess);
	
	if(msgId == kDBMsgPlayerInfoRequest) {
		DBMsgPlayerInfoRequest* req = new DBMsgPlayerInfoRequest;
		req->decode(m_proto, &reader);
		m_dbService->sendMsg(0, msgId, conn->getId(), req);
	}
	else if (msgId == kDBMsgAddRegisterPlayerRequest) {
		DBMsgAddRegisterPlayerRequest* req = new DBMsgAddRegisterPlayerRequest;
		req->decode(m_proto, &reader);
		m_dbService->sendMsg(0, msgId, conn->getId(), req);
	}
	else if (msgId == kDBMsgActivatePlayerRequest) {
		DBMsgActivatePlayerRequest* req = new DBMsgActivatePlayerRequest;
		req->decode(m_proto, &reader);
		m_dbService->sendMsg(0, msgId, conn->getId(), req);
	}
	else if (msgId == kDBMsgSavePlayerInfo) {
		DBMsgSavePlayerInfo* req = new DBMsgSavePlayerInfo;
		req->decode(m_proto, &reader);
		m_dbService->sendMsg(0, msgId, conn->getId(), req);
	}
	
}


void NetworkService::sendConnectionData(int connId, void* data, int len) {
	NetworkServerSendCmd* cmd = new NetworkServerSendCmd;
	cmd->connId = connId;
	cmd->data = new char[len];
	cmd->len = len;
	memcpy(cmd->data, data, len);
	m_service->sendCommand("send", cmd);
}

void NetworkService::handleCommand(std::string cmd, void* data) {
	if (cmd == "send") {
		NetworkServerSendCmd* cmd = (NetworkServerSendCmd*)data;
		auto iter = m_mapConnectin.find(cmd->connId);
		unsigned char buf[4] = {};
		intToBuffer(cmd->len, buf);
		if (iter != m_mapConnectin.end()) {
			iter->second->sendData((char*)buf, 4);
			iter->second->sendData((char*)cmd->data, cmd->len);
		}
		delete cmd->data;
		delete cmd;
	}
}
void NetworkService::handleServiceTimer(int timerId) {

}